#include "gamescene.h"
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsItem>
#include <algorithmutil.h>

GameScene::GameScene(QObject *parent)
    : QGraphicsScene{parent}
{

    for(int i=0;i<9;i++){
        for(int k=0;k<10;k++){
            pieceBoard[i][k]=nullptr;
        }
    }
}

void GameScene::initGameScene()
{
    this->chessData=new ChessData();

    //this->pHash=new QHash<int,ChessPiece*>();

     this->chessData->resetGame(true);

}

GameScene::~GameScene()
{
    if(chessData!=nullptr){
        delete chessData;
    }
    foreach(QGraphicsItem* item, this->items()){
        this->removeItem(item);
    }
    for(int i=0;i<9;i++){
        for(int k=0;k<10;k++){
            if(pieceBoard[i][k]!=nullptr){
                delete pieceBoard[i][k];
            }
        }
    }
    if(this->desktopItem!=nullptr){
        delete desktopItem;
    }
}

const ChessData *GameScene::getChessData()
{
    return this->chessData;
}

ChessData *GameScene::getChessDataPtr()
{
    return this->chessData;
}

void GameScene::saveSxSy(double sx,double sy,double r,double d)
{
    this->chessData->setSpos(sx,sy);
    this->chessData->setRd(r,d);
}

void GameScene::printBoardInfo()
{
    for(int i=0;i<9;i++){
        for(int k=0;k<10;k++){
            if(pieceBoard[i][k]!=nullptr){
                ChessPiece* ped=pieceBoard[i][k];
                //qDebug()<<"ChessPiece:"<<"i_k="<<i<<"_"<<k<<":"<<ped->getTextName()<<"||"<<ped->getCx()<<"_"<<ped->getCy();
            }else{
                qDebug()<<"chessPiece: no data:"<<i<<"_"<<k;
            }
        }
    }
}

void GameScene::saveChessPiece(ChessPiece *cp,int col,int row)
{
   // this->pHash->insert(cp->getId(),cp);
    //qDebug()<<"saveChessPiece::"<<col<<"_"<<row;
   this->pieceBoard[col][row]=cp;
}

void GameScene::cancelSelectPiece(int col, int row)
{
    ChessPiece *cp=this->pieceBoard[col][row];
    cp->setSelected(false);
}

void GameScene::saveDesktopItem(DesktopItem *item)
{
    this->desktopItem=item;
}





void GameScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{

    QPointF pf=event->scenePos();



    for (QGraphicsItem *item : items(event->scenePos())) {
       int t= item->type();
        qDebug()<<"scene event type="<<t;
    }
    qDebug()<<"scene event"<<pf.x()<<","<<pf.y();
    double dx=pf.x()-this->chessData->sx;
    double dy=pf.y()-this->chessData->sy;
    qDebug()<<"scene event dd="<<dx<<","<<dy<<"r="<<chessData->r;
    int nx=AlgorithmUtil::calcPos(pf.x(),chessData->sx,8,chessData->r);
    int ny=AlgorithmUtil::calcPos(pf.y(),chessData->sy,9,chessData->r);
    qDebug()<<"nxy="<<nx<<"_"<<ny;
    if(nx==-1||ny==-1){
        qDebug()<<"select error";
        return;
    }
    ChessPiece* cp=this->pieceBoard[nx][ny];

    if(cp!=nullptr){
        //选中棋子
        qDebug()<<"select one";
        this->printBoardInfo();
        if(chessData->selectId!=-1){
            //已有棋子选中,,
            if(cp->getId()!=chessData->selectId){
                //不同棋子则判断是否可已吃掉后移动
                if(cp->getSide()==chessData->selectSide){

                    //同阵营棋子,切换选中 (重新计算可移动点位)
                    this->cancelSelectPiece(chessData->col,chessData->row);
                    //保存数据
                    chessData->cancelSelectPiece();

                    //新选中棋子
                    chessData->saveSelectPieceInfo(cp->getId(),cp->getCx(),cp->getCy(),cp->getSide(),cp->getKind());
                    cp->setSelected(true);
                    //重新计算可移动点位
                    chessData->calcCanMovePos();
                    this->desktopItem->setPossiblePos(*chessData->canMovePos);
                    this->update();

                }else{
                    //不同棋子则判断是否可已吃掉后移动
                    //chessData->canMoveToPos()
                    if(chessData->canEatPiece(nx,ny)){
                        ChessPiece* srcp=pieceBoard[chessData->col][chessData->row];
                        //pieceBoard[chessData->col][chessData->row]=nullptr;
                        delete cp;
                        pieceBoard[nx][ny]=srcp;
                        pieceBoard[chessData->col][chessData->row]=nullptr;
                        chessData->saveMoveInfo(chessData->col,chessData->row,nx,ny);
                        chessData->cancelSelectPiece();
                        //移动棋子
                        srcp->newPos(nx,ny);
                        this->update();
                    }
                }
            }else{
                //相同棋子则取消选中 (清空可移动点位)
                chessData->cancelSelectPiece();
                this->desktopItem->clearPossiblePos();
                cp->setSelected(false);
                this->update();
            }
        }else{
            //没有棋子选中,则选中棋子 (计算可移动点位)
            qDebug()<<"select piece:"<<cp->getCx()<<"_"<<cp->getCy()<<"||"<<nx<<"_"<<ny;
            if(cp->getSide()==chessData->sideStep){
                chessData->saveSelectPieceInfo(cp->getId(),cp->getCx(),cp->getCy(),cp->getSide(),cp->getKind());
                cp->setSelected(true);
                chessData->calcCanMovePos();
                this->desktopItem->setPossiblePos(*chessData->canMovePos);
                this->update();
            }
        }
    }else{
        qDebug()<<"select no";
        //没有选中棋子,判断是否可以移动
        if(chessData->selectId!=-1){

            //移动棋子
            cp=pieceBoard[chessData->col][chessData->row];
            qDebug()<<"move piece:"<<cp->getCx()<<"_"<<cp->getCy()<<"||"<<nx<<"_"<<ny;
            if(cp==nullptr){
                qDebug()<<"程序出错";
            }else{
                if(chessData->canMoveToPos(nx,ny)){
                    pieceBoard[chessData->col][chessData->row]=nullptr;
                    pieceBoard[nx][ny]=cp;
                    chessData->saveMoveInfo(chessData->col,chessData->row,nx,ny);
                    chessData->cancelSelectPiece();
                    //移动棋子
                    cp->newPos(nx,ny);
                    this->update();
                }else{
                    //不能移动
                    qDebug()<<"不能移动";
                }
            }
        }else{
            // do noting
        }
    }
    cp=nullptr;





}
